package com.cmos.controller;

import com.cmos.entity.User;
import com.cmos.entity.UserRole;
import com.cmos.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("user")
public class UserController {
    //注入用户Service
    @Resource
    private UserService userService;

    //登录
    @RequestMapping("login")
    public Map login(HttpSession session, User user) {
        Map map = new HashMap();
        try {
            if (user.getUsername() != null && user.getPassword() != null) {
                User u = userService.queryUser(new User().setUsername(user.getUsername()));
                if (u != null) {
                    if (user.getPassword().equals(u.getPassword())) {
                        session.setAttribute("user", u);
                        map.put("status", 200);
                        map.put("message", "登录成功");
                    } else {
                        map.put("status", -200);
                        map.put("message", "密码错误");
                    }
                } else {
                    map.put("status", -200);
                    map.put("message", "用户名不存在");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "登录异常");
        }
        return map;
    }

    //登出
    @RequestMapping("logout")
    public Map logout(HttpSession session) {
        Map map = new HashMap();
        try {
            session.invalidate();
            map.put("status", 200);
            map.put("message", "登出成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "登出异常");
        }
        return map;
    }

    //查看个人信息
    @RequestMapping("findById")
    public Map findById(String id) {
        Map map = new HashMap();
        try {
            if (id != null) {
                User user = userService.queryUser(new User().setId(id));
                map.put("user", user);
                map.put("status", 200);
            } else {
                map.put("status", -200);
                map.put("message", "查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //查询所有用户
    @RequestMapping("findAllUser")
    public Map findAllUser() {
        Map map = new HashMap();
        try {
            List<User> users = userService.queryAllUser();
            map.put("status", 200);
            map.put("users", users);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //新增用户
    @RequestMapping("addUser")
    public Map addUser(User user, String[] ids) {
        Map map = new HashMap();
        try {
            user.setId(UUID.randomUUID().toString().replace("-", ""));
            userService.saveUser(user);
            //为用户添加角色
            addRoleToUser(user.getId(), ids);
            map.put("status", 200);
            map.put("message", "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //修改用户信息
    @RequestMapping("updateUser")
    public Map updateUser(HttpSession session, User user, String[] ids) {
        Map map = new HashMap();
        try {
            //修改用户信息
            userService.changeUser(user);
            //修改用户角色
            if (ids != null && ids.length > 0) {
                updateRoleForUser(user.getId(), ids);
            }
            //修改成功后刷新session中的user
            User u = userService.queryUser(user);
            session.setAttribute("user", u);
            map.put("status", 200);
            map.put("message", "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");

        }
        return map;
    }

    //删除用户
    @RequestMapping("delUser")
    public Map delUser(String id) {
        Map map = new HashMap();
        try {
            userService.removeUser(id);
            //在user_role表中删除用户的角色
            userService.removeUserRole(id);
            map.put("status", 200);
            map.put("message", "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", -200);
            map.put("message", "数据异常");
        }
        return map;
    }

    //给用户增加角色
    public Integer addRoleToUser(String userId, String[] ids) {
        List<UserRole> userRoles = new ArrayList<>();
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                UserRole userRole = new UserRole();
                userRole.setId(UUID.randomUUID().toString().replace("-", ""))
                        .setUserId(userId)
                        .setRoleId(id);
                userRoles.add(userRole);
            }
        }
        return userService.saveUserRole(userRoles);
    }

    //修改用户角色
    public Integer updateRoleForUser(String uesrId, String[] ids) {
        //删除用户所有角色
        userService.removeUserRole(uesrId);
        //为用户添加角色
        return addRoleToUser(uesrId, ids);
    }
}
